package com.power.project.service.impl;

import com.power.base.service.impl.BaseServiceImpl;
import com.power.common.exception.CustomBizException;
import com.power.project.dao.Ww_tagDao;
import com.power.project.dao.Ww_usertagDao;
import com.power.project.model.Ww_tag;
import com.power.project.model.Ww_usertag;
import com.power.project.model.bo.TagSaveBO;
import com.power.project.model.bo.TagUserSetBO;
import com.power.project.model.dto.UserTagDTO;
import com.power.project.model.pageInfo.Ww_tagPageInfo;
import com.power.project.model.vo.TagVO;
import com.power.project.model.vo.feignvo.Tag;
import com.power.project.model.vo.feignvo.TagUser;
import com.power.project.service.Ww_tagService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签业务层实现
 */
@Service
public class Ww_tagServiceImpl extends BaseServiceImpl implements Ww_tagService {

    @Autowired
    private Ww_tagDao dao;

    @Autowired
    private Ww_usertagDao userTagDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(TagSaveBO saveBO) {
        // save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (StringUtils.isNotEmpty(saveBO.getTagId())) {
            Ww_tag tag = dao.getByTagId(saveBO.getTagId());
            if (tag != null && !tag.getName().equals(saveBO.getName())) {
                tag.setName(saveBO.getName());
                dao.save(tag);
                return;
            }
        }
        Assert.notNull(saveBO.getTenantId(), "租户id不能为空");
        Ww_tag tag = new Ww_tag(saveBO);
        dao.save(tag);
    }

    @Override
    public void removeById(String tagId) {
        dao.deleteByTagId(tagId);
    }

    @Override
    public Page<TagVO> findAll(Ww_tagPageInfo pageInfo) {
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber() <= 1 ? 0 : pageInfo.getPageNumber() - 1, pageInfo.getPageSize());

        return dao.findAll(pageInfo, pageable);
    }

    @Override
    public List<TagVO> findAllTags(String tenantId) {
        return dao.findAllTags(tenantId);
    }

    @Override
    public List<String> getUserIds(String tagId) {
        Ww_tag tag = dao.getByTagId(tagId);
        if (tag == null) {
            throw new CustomBizException("标签不存在");
        }
        List<UserTagDTO> list = userTagDao.getByTagId(tag.getTagid(), tag.getTenantid());
        return list.stream().map(UserTagDTO::getUserId).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSet(TagUserSetBO setBO) {
        Ww_tag tag = dao.getByTagId(setBO.getTagId());
        if (tag == null) {
            throw new CustomBizException("标签不存在");
        }
        //移除该标签下所有的人员
        userTagDao.deleteByTagIdAndTenantId(tag.getTagid(), tag.getTenantid());

        if (CollectionUtils.isNotEmpty(setBO.getUserIds())) {
            //重新添加人员
            List<Ww_usertag> list = setBO.getUserIds().stream().map(t -> new Ww_usertag(tag, t)).collect(Collectors.toList());
            userTagDao.saveAll(list);
        }
    }

    @Override
    public List<Tag> queryAllTagWechat(String tenantId) {
        List<Ww_tag> list = dao.queryAllTagWechat(tenantId);
        return list.stream().map(Tag::new).collect(Collectors.toList());
    }

    @Override
    public List<TagUser> queryAllTagUserWechat(String tenantId) {
        List<Ww_usertag> list = userTagDao.queryAllTagUserWechat(tenantId);
        return list.stream().map(TagUser::new).collect(Collectors.toList());
    }
}
